EF 核心 2.1.4 FileLoadException System.ComponentModel.Annotations 4.2.0.0
EF Core 2.1.4 FileLoadException System.ComponentModel.Annotations 4.2.0.0
将我们的数据访问层从 EF6 升级到 EF Core 2.1(.4) 后,我们遇到了 FileLoadException
和 System.ComponentModel.Annotations, Version=4.2.0.0
的问题,这很奇怪,因为 EF Core 2.1.4 使用 4.5.0.0
,我们的解决方案中没有其他(我们可以找到)使用 4.2.0.0
。
例如,我们的解决方案中有以下项目结构:
DataAccess:(所有项目使用 Microsoft.EntityFrameworkCore.SqlServer 版本 2.1.4)
- 模型(仅限使用 Scaffold-DbContext 创建的模型)
- 核心(抽象提供者、持久化者等)
- 常见(DTO、具体提供者、持久性等)
主要解决方案:(没有项目安装 EF Core 包)
- 一些项目 (A) 的项目引用模型、核心、通用
例如,即使项目 A 是一个简单的控制台应用程序,它只是从数据库加载一些东西并将其显示到控制台,当在 DataAccess.Core 中的抽象提供者级别评估某些东西时,我们得到以下例外:
System.IO.FileLoadException occurred HResult=0x80131040
Message=Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
奇怪的是,我们到处都找不到System.ComponentModel.Annotations, Version=4.2.0.0
,版本在Nuget中甚至被跳过了。
在这个问题中尝试接受的答案:Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.1.0.0
,通过将以下内容添加到我的 .csproj
文件似乎解决了某些项目的问题,但不是全部:
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
在构建后生成的 DataAccess.Models.dll.config 文件中,对 System.ComponentModel.Annotations
的唯一引用如下:
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
</dependentAssembly>
这不是 4.2.0.0
,但我们不确定新版本号是在哪里决定的。这可能是我们遗漏或配置不正确的一些小问题,但我们将不胜感激在查找问题方面提供的帮助或在何处查找的指示。我们不必将 AutoGenerateBindingRedirects
添加到每个 .csproj
文件的解决方案是理想的
来自@JRB 评论的内容,格式为可读性的答案:
这是一个微软似乎很难解决的现有问题,可能是因为它涉及多个开发组之间的合作。它只出现在一些更复杂的解决方案中。解决方案非常简单。将 bindingRedirect
从
更改为
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
到
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.0.0.0" />
而且您的问题消失的可能性很小。此绑定重定向可以自动应用于多个项目。测试是哪个(些)问题导致了您的问题。
我遇到了同样的问题。我已经通过在开始时实现以下辅助函数重定向程序集来解决它( 中建议):
public static class FunctionsAssemblyResolver
{
#region Public Methods
public static void RedirectAssembly()
{
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssemblyOnCurrentDomain;
}
#endregion Public Methods
#region Private Methods
private static Assembly ResolveAssemblyOnCurrentDomain(object sender, ResolveEventArgs args)
{
var requestedAssembly = new AssemblyName(args.Name);
var assembly = default(Assembly);
AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssemblyOnCurrentDomain;
try
{
assembly = Assembly.Load(requestedAssembly.Name);
}
catch
{ }
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssemblyOnCurrentDomain;
return assembly;
}
#endregion Private Methods
}
将我们的数据访问层从 EF6 升级到 EF Core 2.1(.4) 后,我们遇到了 FileLoadException
和 System.ComponentModel.Annotations, Version=4.2.0.0
的问题,这很奇怪,因为 EF Core 2.1.4 使用 4.5.0.0
,我们的解决方案中没有其他(我们可以找到)使用 4.2.0.0
。
例如,我们的解决方案中有以下项目结构:
DataAccess:(所有项目使用 Microsoft.EntityFrameworkCore.SqlServer 版本 2.1.4)
- 模型(仅限使用 Scaffold-DbContext 创建的模型)
- 核心(抽象提供者、持久化者等)
- 常见(DTO、具体提供者、持久性等)
主要解决方案:(没有项目安装 EF Core 包)
- 一些项目 (A) 的项目引用模型、核心、通用
例如,即使项目 A 是一个简单的控制台应用程序,它只是从数据库加载一些东西并将其显示到控制台,当在 DataAccess.Core 中的抽象提供者级别评估某些东西时,我们得到以下例外:
System.IO.FileLoadException occurred HResult=0x80131040 Message=Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
奇怪的是,我们到处都找不到System.ComponentModel.Annotations, Version=4.2.0.0
,版本在Nuget中甚至被跳过了。
在这个问题中尝试接受的答案:Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.1.0.0
,通过将以下内容添加到我的 .csproj
文件似乎解决了某些项目的问题,但不是全部:
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
在构建后生成的 DataAccess.Models.dll.config 文件中,对 System.ComponentModel.Annotations
的唯一引用如下:
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
</dependentAssembly>
这不是 4.2.0.0
,但我们不确定新版本号是在哪里决定的。这可能是我们遗漏或配置不正确的一些小问题,但我们将不胜感激在查找问题方面提供的帮助或在何处查找的指示。我们不必将 AutoGenerateBindingRedirects
添加到每个 .csproj
文件的解决方案是理想的
来自@JRB 评论的内容,格式为可读性的答案:
这是一个微软似乎很难解决的现有问题,可能是因为它涉及多个开发组之间的合作。它只出现在一些更复杂的解决方案中。解决方案非常简单。将 bindingRedirect
从
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
到
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.0.0.0" />
而且您的问题消失的可能性很小。此绑定重定向可以自动应用于多个项目。测试是哪个(些)问题导致了您的问题。
我遇到了同样的问题。我已经通过在开始时实现以下辅助函数重定向程序集来解决它(
public static class FunctionsAssemblyResolver
{
#region Public Methods
public static void RedirectAssembly()
{
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssemblyOnCurrentDomain;
}
#endregion Public Methods
#region Private Methods
private static Assembly ResolveAssemblyOnCurrentDomain(object sender, ResolveEventArgs args)
{
var requestedAssembly = new AssemblyName(args.Name);
var assembly = default(Assembly);
AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssemblyOnCurrentDomain;
try
{
assembly = Assembly.Load(requestedAssembly.Name);
}
catch
{ }
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssemblyOnCurrentDomain;
return assembly;
}
#endregion Private Methods
}